<!doctype html>
<html ng-app="app">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../../build/common/base.css" rel="stylesheet">
<script type="text/javascript" src="../../build/common/base.js"></script>
<script type="text/javascript" src="../../build/common/ngEdit.js"></script>
</head>
<script type="text/javascript">
	var id = $.getParam("id");
	var app = angular.module('app', [ 'baseDirective' ]);
	app.controller("ctrl", [ '$scope', 'baseService','ArrayToolService', 
		function($scope, baseService,ArrayToolService) {
			$scope.ArrayTool = ArrayToolService;
			$scope.isSaveEd = id;
			if(id){
				//$("#orderNum").attr("readonly","readonly");
			}else{
			}
		
			/**
			 * 数据初始化。
			 */
			$scope.init=function(){
				if(!$scope.data){
					$scope.data={};
				}
			};
			
			$scope.selectProductRelation = function(){
				if(!$scope.data){
		 			$scope.data={};
		 		}
		 		if(!$scope.data.customerRelationList){
		 			$scope.data.customerRelationList = [];
		 		}
		 		var arr = [];
		 		var newList = [];
				for(var i=0,relation;relation = $scope.data.customerRelationList[i++];){
					if(relation.type == "contractProduct"){
						var object = {};
						object.id = relation.productId;
						object.name = relation.productName;
						arr.push(object);
					}else{
						newList.push(relation);
					}
				}
		 		
		 		CustUtil.openCustDialog("productSelector",function(data,innerWindow){
					$scope.$apply(function(){
						$scope.data.customerRelationList = newList;
						
						//将重新选择的放入到List中
						for(var i = 0; i < data.length; i++) {
							var rights = {
								type :"contractProduct",
								productId: data[i].id,
								productName : data[i].name,
								};
							$scope.data.customerRelationList.push(rights);
						}
						});
				   $.Dialog.close(innerWindow);
				},arr);
		 	}
			
			$scope.selectCustomerList = function(){
		 		if(!$scope.data.customerList){
		 			$scope.data.customerList = [];
		 		}
		 		var arr = [];
		 		var newList = [];
				for(var i=0,customer;customer = $scope.data.customerList[i++];){
					var object = {};
					object.id = customer.id;
					object.name = customer.name;
					arr.push(object);
				}
		 		
		 		CustUtil.openCustDialog("getMyUsableCustomers",function(data,innerWindow){
					$scope.$apply(function(){
						$scope.data.customerList = newList;
						
						//将重新选择的放入到List中
						for(var i = 0; i < data.length; i++) {
							var rights = {
								id : data[i].id,
								name : data[i].name,
								};
							$scope.data.customerList.push(rights);
						}
						});
				   $.Dialog.close(innerWindow);
				},arr);
		 	}
			
			} ]);

	/* 获取URL表单的 JSON 数据  必须以 getData为方法名*/
	function getData() {
		return AngularUtil.getScope().data;
	}
	/* 获取URL表单的校验情况，必须以isValid 为方法名 */
	function isValid() {
		//校验表单 ab-validate的 校验
		if (!ngUtil.getScope().form.$valid) {
			$.Dialog.error("表单校验不通过！");
			return false;
		}

		// 您可以自由的去做特殊业务逻辑的校验
		 if (!ngUtil.getScope().data.customerList || ngUtil.getScope().data.customerList.length===0) {
			$.Dialog.error("成交客户不可为空！");
			return false;
		}
		return true; 
	}
</script>
<body class="panel success" ng-controller="ctrl" id="customerContractEdit">
	<!-- 顶部按钮 -->
	<!-- <div class="panel-heading">
		<a class="btn btn-primary fa-save" ng-model="data"
			ab-save="/crm/customerContract/save"> 保存 </a> <a href="javascript:void(0)"
			class="btn btn-sm btn-primary fa-reply"
			onclick="javascript:$.Dialog.close(window);"> 返回 </a>
	</div>  -->
	<!--  ab-load 去controller 层获取业务数据  -->
	<form name="form" method="post" ab-load="crm/customerContract/get?id={id}"
		ng-model="data">

		<table class="form-table">
			<tbody>
				<tr class="firstRow">
					<th>合同编号</th>
					<td><input type="text" class=" form-control"
						ng-model="data.orderNum" id="orderNum" ng-disabled="data.id!=null"
						desc="合同编号" ab-validate="{&quot;required&quot;:true}" /></td>
				</tr>
				<tr>
					<th>
						<span>成交客户</span>
						<span class="required">*</span>
					</th>
					<td>
						<span ng-repeat="customer in data.customerList" style="margin-right: 10px" class="label label-danger"> {{customer.name}}
						<button ng-show="data.id==null" href="javascript:void(0)" class="btn btn-link btn-default btn-xs" ng-click="ArrayTool.remove(customer,data.customerList)">×</button></span>
						<span ng-show="data.id==null" class="btn  btn-sm btn-primary fa fa-search" ng-click="selectCustomerList()">选择</span> 
					</td>
				</tr>
				<tr>
					<th>主题</th>
					<td><input type="text" class=" form-control"
						ng-model="data.subject" ng-disabled="data.id!=null"
						desc="主题" ab-validate="{}" /></td>
				</tr>
				<tr>
					<th>签约时间</th>
					<td><input class=" form-control"
						ng-model="data.contractTime" ab-validate="{}"
						desc="签约时间" ng-disabled="data.id!=null"
						ab-date="yyyy-MM-dd HH:mm:ss" /></td>
				</tr>
				<tr>
					<th>开始时间</th>
					<td><input class=" form-control"
						ng-model="data.startTime" ab-validate="{}"
						desc="开始时间" ng-disabled="data.id!=null"
						ab-date="yyyy-MM-dd HH:mm:ss" /></td>
				</tr>
				<tr>
					<th>结束时间</th>
					<td><input class=" form-control"
						ng-model="data.endTime" ab-validate="{}"
						desc="结束时间" ng-disabled="data.id!=null"
						ab-date="yyyy-MM-dd HH:mm:ss" /></td>
				</tr>
				
				
				<tr >
					<th>是否含税</th>
					<td><div ng-model="data.isTax"
							ab-validate="{}" desc="是否含税">
							<label class=" radio-inline"><input type="radio" class="typeInput" ng-disabled="data.id!=null"
								ng-model="data.isTax" value="0" />否</label><label
								class=" radio-inline"><input type="radio" class="typeInput" ng-disabled="data.id!=null"
								ng-model="data.isTax" value="1" />是</label>
						</div></td>
				</tr>
				<tr >
					<th>是否提供发票</th>
					<td><div ng-model="data.isInvoice"
							ab-validate="{}" desc="是否提供发票">
							<label class=" radio-inline"><input type="radio" class="typeInput" ng-disabled="data.id!=null"
								ng-model="data.isInvoice" value="0" />否</label><label
								class=" radio-inline"><input type="radio" class="typeInput" ng-disabled="data.id!=null"
								ng-model="data.isInvoice" value="1" />是</label>
						</div></td>
				</tr>
				<tr>
					<th>总金额</th>
					<td><input type="text" class=" form-control"
						ng-model="data.money" ng-disabled="data.id!=null"
						desc="总金额" ab-validate="{'number':true}" /></td>
				</tr>
				<tr>
					<th>备注</th>
					<td><input type="text" class=" form-control"
						ng-model="data.remark" ng-disabled="data.id!=null"
						desc="备注" ab-validate="{}" /></td>
				</tr>
				<tr>
					<th>
						<span>包含产品:</span>
					</th>
					<td>
						<span ng-repeat="relation in data.customerRelationList |filter:{type:'contractProduct'}" style="margin-right: 10px" class="label label-danger"> {{relation.productName}}
						<button ng-show="data.id==null" href="javascript:void(0)" class="btn btn-link btn-default btn-xs" ng-click="ArrayTool.remove(relation,data.customerRelationList)">×</button></span>
						<span ng-show="data.id==null" class="btn  btn-sm btn-primary fa fa-search" ng-click="selectProductRelation()">选择</span> 
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</body>
</html>